import config from "@/request/config.js"
import {getDictionary} from "@/api/common.js"
/**
 * 格式化时间
 * @param {String} dateValue 时间
 * @param {String} format  格式
 * @param {Boolean} isWeek 是否返回周末
 */
export function formatDate(dateValue, format, isWeek) {
	if (typeof format === "undefined" || format === null) {
		format = "Y-m-d h:i:s";
	}
	if (dateValue == null || dateValue == "") return "";
	var d = new Date(dateValue);

	const date = {
		Y: d.getFullYear(),
		"m+": d.getMonth() + 1 < 10 ? `0${d.getMonth() + 1}` : d.getMonth() + 1,
		"d+": d.getDate() < 10 ? `0${d.getDate()}` : d.getDate(),
		"h+": d.getHours() < 10 ? `0${d.getHours()}` : d.getHours(),
		"i+": d.getMinutes() < 10 ? `0${d.getMinutes()}` : d.getMinutes(),
		"s+": d.getSeconds() < 10 ? `0${d.getSeconds()}` : d.getSeconds(),
		"q+": Math.floor((d.getMonth() + 3) / 3),
		"S+": d.getMilliseconds(),
	};

	for (let k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ?
				date[k] :
				("00" + date[k]).substr(("" + date[k]).length)
			);
		}
	}
	if (isWeek) {
		const week = this.weekList[d.getDay()];
		format = format + week;
	}
	return format;
}

/*加*/
export function enCodeUrl(params) {
	return encodeURIComponent(JSON.stringify(params))
}
/*解*/
export function deCodeUrl(params) {
	return JSON.parse(decodeURIComponent(params))
}

/*资源地址拼接*/
export function formatLink(url) {
	if (url) {
		if (url.indexOf("http://") >= 0 || url.indexOf("https://") >= 0) {
			return url
		} else {
			return config.baseUrl + url
		}
	} else {
		return url
	}
}

//获取字典列表
export async function getDictionaryData(name) {
	let list = await new Promise(function(resolve, reject) {
		getDictionary(name).then((res) => {
			if (res.code == 200) {
				resolve(res.data);
			}
		})
	})
	return list
}